/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerCadastraProdutos;
import java.math.BigDecimal;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import model.Produto;
import model.Unidade;
import util.Util;

/**
 *
 * @author Dick developer
 */
public class JCadastrarProduto extends javax.swing.JFrame {

    private ControllerCadastraProdutos ccp;
    private JPrincipal jp;
    private Produto produto;
            
    int valor_id;
    
    public JCadastrarProduto(ControllerCadastraProdutos ccp,JPrincipal jp ) {
        initComponents();
        this.ccp = ccp;
        this.jp = jp;
        
        DefaultComboBoxModel model = (DefaultComboBoxModel)cb_unidade.getModel();
        for(Unidade u: ccp.listaComUnidades()){        
            model.addElement(u);
        }  
            
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel9 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        quantidade = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        cod_barras = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        valor_compra = new javax.swing.JFormattedTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        valor_venda = new javax.swing.JFormattedTextField();
        cb_unidade = new javax.swing.JComboBox();
        nome_prod = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel9.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        jLabel9.setText("Cadastro de Produtos");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 20, -1, -1));

        jLabel2.setText("Foto do Produto");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 90, -1, -1));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 110, 210, 10));

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator4, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 120, 30, 180));

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 120, -1, 180));
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 120, 190, 180));

        jLabel6.setText("Quantidade estoque:");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 150, -1, -1));

        quantidade.setDocument(new util.NumberField());
        quantidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quantidadeActionPerformed(evt);
            }
        });
        getContentPane().add(quantidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 150, 130, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fechar.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton1MouseExited(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 350, 60, 60));

        jButton2.setForeground(new java.awt.Color(255, 255, 255));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton2MouseEntered(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton2MouseExited(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 350, 60, 60));

        jLabel4.setText("Código de barras:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 310, -1, -1));

        cod_barras.setDocument(new util.NumberField(13));
        cod_barras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cod_barrasActionPerformed(evt);
            }
        });
        getContentPane().add(cod_barras, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 310, 130, -1));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cod_barras.png"))); // NOI18N
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 300, 40, 30));

        jLabel5.setText("Nome produto:");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 110, -1, -1));

        jLabel11.setText("Valor de Compra:");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 190, -1, -1));

        valor_compra.setDocument(new util.MonetarioField());
        valor_compra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valor_compraActionPerformed(evt);
            }
        });
        getContentPane().add(valor_compra, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 190, 130, -1));

        jLabel12.setText("Valor da unidade:");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 270, -1, -1));

        jLabel13.setText("Valor de Venda:");
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 230, -1, -1));

        valor_venda.setDocument(new util.MonetarioField());
        valor_venda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valor_vendaActionPerformed(evt);
            }
        });
        getContentPane().add(valor_venda, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 230, 130, -1));

        cb_unidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_unidadeActionPerformed(evt);
            }
        });
        getContentPane().add(cb_unidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 270, 130, -1));

        nome_prod.setDocument(new util.UpperField());
        nome_prod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nome_prodActionPerformed(evt);
            }
        });
        getContentPane().add(nome_prod, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 110, 130, -1));

        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fundotelasinvertido.png"))); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 700, 440));

        jMenuBar1.setBackground(java.awt.Color.white);

        jMenu1.setBackground(java.awt.Color.white);
        jMenu1.setText("Ajuda");
        jMenuBar1.add(jMenu1);

        jMenu2.setBackground(java.awt.Color.white);
        jMenu2.setText("Regras da casa");
        jMenuBar1.add(jMenu2);

        jMenu3.setBackground(java.awt.Color.white);
        jMenu3.setText("Sair");

        jMenuItem1.setText("Sair");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void quantidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quantidadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_quantidadeActionPerformed

    private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseEntered

        jButton1.setSize(70,70);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseEntered

    private void jButton1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseExited

        jButton1.setSize(60,60);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseExited

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jp.restaurar(jp);
        this.hide();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseEntered

        jButton2.setSize(70,70);

    }//GEN-LAST:event_jButton2MouseEntered

    private void jButton2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseExited

        jButton2.setSize(60,60);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseExited

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
      
        produto = new Produto();
        produto.setDescricao(nome_prod.getText().toString());
        produto.setEstoque(Integer.parseInt(quantidade.getText()));        
        produto.setVlrCompra(Util.StrintToBigDecimal(valor_compra.getText()));        
        produto.setVlrVenda(Util.StrintToBigDecimal(valor_venda.getText())); 
        produto.setUnidade((Unidade) cb_unidade.getSelectedItem());       
        produto.setCodigoBarras(cod_barras.getText().toString());
        
        if(ccp.cadastra_produto(produto)){
        
            JOptionPane.showMessageDialog(null, "Novo Produto cadastrado com sucesso. ");
            this.zera_tela();
        
        }else{
            
            JOptionPane.showMessageDialog(null, "Erro ao cadastrar novo produto. Tente novamente. ");
            
        }
        
        

        
        
        
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void cod_barrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cod_barrasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cod_barrasActionPerformed

    private void valor_compraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valor_compraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valor_compraActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        jp.restaurar(jp);
        this.hide();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void valor_vendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valor_vendaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valor_vendaActionPerformed

    private void nome_prodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nome_prodActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nome_prodActionPerformed

    private void cb_unidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_unidadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_unidadeActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_unidade;
    private javax.swing.JFormattedTextField cod_barras;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JFormattedTextField nome_prod;
    private javax.swing.JFormattedTextField quantidade;
    private javax.swing.JFormattedTextField valor_compra;
    private javax.swing.JFormattedTextField valor_venda;
    // End of variables declaration//GEN-END:variables

    public void exibe() {
        setVisible(true);
    }



    private void zera_tela(){
        nome_prod.setText("");
        quantidade.setText("");
        valor_compra.setText("");
        valor_venda.setText("");
        cb_unidade.setSelectedItem(1);
        cod_barras.setText("");
        
    }








}
